<?php

namespace App\Listeners;

use App\Events\SendSms;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Cache;
use Overtrue\EasySms\EasySms;

class SendSmsListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  \App\Events\SendSms  $event
     * @return void
     */
    public function handle(SendSms $event)
    {
//        发送验证码到手机
        $config = config('sms');

        $easySms = new EasySms($config);

        $code = rand(1000, 9999);

//        缓存手机号对应的code
        Cache::put('phone_code_' . $event->phone, $code, now()->addMinutes(10));

        $easySms->send($event->phone, [
            'template' => $config['template'],
            'data' => [
                'code' => $code,
                'product' => $event->product,
            ],
        ]);
    }
}
